package pers.acp.test.application.task

import io.gitee.zhangbinhub.acp.boot.job.AcpJob
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.boot.websocket.AcpWebSocketClient
import io.gitee.zhangbinhub.acp.boot.websocket.AcpWebSocketServer
import org.noear.solon.annotation.Component
import org.noear.solon.annotation.Inject
import org.noear.solon.core.handle.Context
import pers.acp.test.application.websocket.TestWebSocketClient
import java.net.URI

@Component(name = "testJob2")
class TestJob2(
    private val log: LogAdapter,
    private val webSocketClient: TestWebSocketClient
) : AcpJob {
    override fun handle(ctx: Context) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>我是定时任务2》》》》》》》》》》")
        testServer()
        testClient()
    }

    private fun testServer() {
        val message = "这是websocket服务端消息"
        log.info("【websocket】广播 >>>>> $message")
        AcpWebSocketServer.sendMessageToClient("$message----> 广播")
        log.info("【websocket】单播【1】 >>>>> $message")
        AcpWebSocketServer.sendMessageToClient("$message----> 单播", "1")
    }

    private fun testClient() {
        val client = webSocketClient.open("123", URI.create("ws://localhost:8443/v1"))
        val message = "这是websocket客户端消息"
        log.info("【websocket】广播 >>>>> $message")
        AcpWebSocketClient.sendMessageToServer("$message----> 广播")
        log.info("【websocket】单播【${client.clientId}】 >>>>> $message")
        client.sendMessage("$message----> 单播(实例)")
        AcpWebSocketClient.sendMessageToServer("$message----> 单播(静态)", client.clientId)
    }
}